import axios from 'axios';

const API_BASE_URL = '/api';

class ApiService {
  // 课程相关API
  async getCourses() {
    const response = await axios.get(`${API_BASE_URL}/courses`);
    return response.data;
  }

  async getCoursesPublic() {
    const response = await axios.get(`${API_BASE_URL}/courses/public`);
    return response.data;
  }

  async getCourseById(id) {
    const response = await axios.get(`${API_BASE_URL}/courses/${id}`);
    return response.data;
  }

  async getCourseByIdPublic(id) {
    const response = await axios.get(`${API_BASE_URL}/courses/public/${id}`);
    return response.data;
  }

  async createCourse(courseData) {
    const response = await axios.post(`${API_BASE_URL}/courses`, courseData);
    return response.data;
  }

  async updateCourse(id, courseData) {
    const response = await axios.put(`${API_BASE_URL}/courses/${id}`, courseData);
    return response.data;
  }

  async updateCourseStatus(id, status) {
    const response = await axios.put(`${API_BASE_URL}/courses/${id}/status`, { status });
    return response.data;
  }

  async deleteCourse(id) {
    const response = await axios.delete(`${API_BASE_URL}/courses/${id}`);
    return response.data;
  }

  async getCoursesByTeacher(teacherId) {
    const response = await axios.get(`${API_BASE_URL}/courses/teacher/${teacherId}`);
    return response.data;
  }

  async getCoursesByStatus(status) {
    const response = await axios.get(`${API_BASE_URL}/courses/public/status/${status}`);
    return response.data;
  }

  async searchCourses(keyword) {
    const response = await axios.get(`${API_BASE_URL}/courses/public/search?keyword=${encodeURIComponent(keyword)}`);
    return response.data;
  }

  async getCoursesByTimeRange(startTime, endTime) {
    const response = await axios.get(`${API_BASE_URL}/courses/time-range?startTime=${startTime}&endTime=${endTime}`);
    return response.data;
  }

  // 预约相关API
  async getBookings() {
    const response = await axios.get(`${API_BASE_URL}/bookings`);
    return response.data;
  }

  async getBookingById(id) {
    const response = await axios.get(`${API_BASE_URL}/bookings/${id}`);
    return response.data;
  }

  async createBooking(bookingData) {
    const response = await axios.post(`${API_BASE_URL}/bookings`, bookingData);
    return response.data;
  }

  async updateBookingStatus(id, status) {
    const response = await axios.put(`${API_BASE_URL}/bookings/${id}/status`, { status });
    return response.data;
  }

  async cancelBooking(id) {
    const response = await axios.put(`${API_BASE_URL}/bookings/${id}/cancel`);
    return response.data;
  }

  async deleteBooking(id) {
    const response = await axios.delete(`${API_BASE_URL}/bookings/${id}`);
    return response.data;
  }

  async getBookingsByStudent(studentId) {
    const response = await axios.get(`${API_BASE_URL}/bookings/student/${studentId}`);
    return response.data;
  }

  async getBookingsByCourse(courseId) {
    const response = await axios.get(`${API_BASE_URL}/bookings/course/${courseId}`);
    return response.data;
  }

  async getBookingsByStatus(status) {
    const response = await axios.get(`${API_BASE_URL}/bookings/status/${status}`);
    return response.data;
  }

  async getConfirmedBookingsCount(courseId) {
    const response = await axios.get(`${API_BASE_URL}/bookings/course/${courseId}/count`);
    return response.data;
  }

  async checkBookingExists(studentId, courseId) {
    const response = await axios.get(`${API_BASE_URL}/bookings/check?studentId=${studentId}&courseId=${courseId}`);
    return response.data;
  }

  // 学生相关API
  async getStudents() {
    const response = await axios.get(`${API_BASE_URL}/students`);
    return response.data;
  }

  async getStudentById(id) {
    const response = await axios.get(`${API_BASE_URL}/students/${id}`);
    return response.data;
  }

  async createStudent(studentData) {
    const response = await axios.post(`${API_BASE_URL}/students`, studentData);
    return response.data;
  }

  async updateStudent(id, studentData) {
    const response = await axios.put(`${API_BASE_URL}/students/${id}`, studentData);
    return response.data;
  }

  async deleteStudent(id) {
    const response = await axios.delete(`${API_BASE_URL}/students/${id}`);
    return response.data;
  }

  async getStudentByStudentId(studentId) {
    const response = await axios.get(`${API_BASE_URL}/students/student-id/${studentId}`);
    return response.data;
  }

  async getStudentByEmail(email) {
    const response = await axios.get(`${API_BASE_URL}/students/email/${email}`);
    return response.data;
  }

  async checkStudentIdExists(studentId) {
    const response = await axios.get(`${API_BASE_URL}/students/check/student-id/${studentId}`);
    return response.data;
  }

  async checkStudentEmailExists(email) {
    const response = await axios.get(`${API_BASE_URL}/students/check/email/${email}`);
    return response.data;
  }

  // 教师相关API
  async getTeachers() {
    const response = await axios.get(`${API_BASE_URL}/teachers`);
    return response.data;
  }

  async getTeacherById(id) {
    const response = await axios.get(`${API_BASE_URL}/teachers/${id}`);
    return response.data;
  }

  async createTeacher(teacherData) {
    const response = await axios.post(`${API_BASE_URL}/teachers`, teacherData);
    return response.data;
  }

  async updateTeacher(id, teacherData) {
    const response = await axios.put(`${API_BASE_URL}/teachers/${id}`, teacherData);
    return response.data;
  }

  async deleteTeacher(id) {
    const response = await axios.delete(`${API_BASE_URL}/teachers/${id}`);
    return response.data;
  }

  async getTeacherByEmail(email) {
    const response = await axios.get(`${API_BASE_URL}/teachers/email/${email}`);
    return response.data;
  }

  async checkTeacherEmailExists(email) {
    const response = await axios.get(`${API_BASE_URL}/teachers/check/email/${email}`);
    return response.data;
  }
}

export default new ApiService();